<?php
class App_Admin_Model_PluginI18n extends Main_Db_Table_Abstract
{
	/**
	 * @var string
	 */
	protected $_name = 'plugin_i18n';
	
	/**
	 * @var string
	 */
	protected $_primary = 'id';
	
	/**
	 * @var array
	 */	
	protected $_fields = array(
		'id',
		'plugin_id',
		'lang_id',
		'title',
		'description',
	);
	
	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data) 
	{
		$dataOk = $this->beforeSave($data, array('plugin_id', 'lang_id', 'title', 'description'
		), array('title', 'description'));
		
		return $this->insert($dataOk);
	}

	/**
	 * @param array $data
	 * @param int $id
	 * @return bool
	 */
	public function updateItem($data, $id) 
	{
		$dataOk = $this->beforeSave($data, array('lang_id', 'title', 'description'
		), array('title', 'description'));
    	
    	$rowCount = $this->getAdapter()->fetchOne("
    		SELECT 
    			COUNT(*) 
    		FROM 
    			plugin_i18n 
    		WHERE 
    			plugin_id={$id} AND lang_id={$dataOk['lang_id']}
    	");
    	
    	if ($rowCount == 1) {
    		$result = $this->update($dataOk, "plugin_id={$id} AND lang_id={$dataOk['lang_id']}");
    	} else if ($rowCount == 0){
    		$dataInsert = $dataOk 
    					+ array(
    						'plugin_id'	=> $id,
    						'lang_id'	=> $dataOk['lang_id']
    					);
    		$result = $this->insert($dataInsert);
    	}
    	return $result;
	}

	/**
	 * @param int $pluginId
	 * @return bool
	 */
	public function deleteItemByPluginId($pluginId) 
	{
		return $this->delete("plugin_id={$pluginId}");
	}
	
}
